package top.shiyiri.java;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Aunean
 * @date 2022/1/10
 */
/*
实现TCP的网络编程
例题三：从客户端发送文件给服务端，服务端保存在本地，
并返回“发送成功”给客户端。并关闭相应的连接

 */
public class TCPTest3 {

    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        BufferedInputStream bis = null;
        ByteArrayOutputStream baos = null;
        try {
            socket = new Socket(InetAddress.getByName("localhost"), 9010);
            os = socket.getOutputStream();

            bis = new BufferedInputStream(new FileInputStream("../data/photo/q.png"));

            //客户端发送文件
            byte[] buffer = new byte[1024];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                os.write(buffer, 0, len);
            }
            //关闭数据输出
            socket.shutdownOutput();

            //客户端接收服务器端信息
            byte[] buffer2 = new byte[1024];
            int len1;
            InputStream is = socket.getInputStream();
            baos = new ByteArrayOutputStream();
            while ((len1 = is.read(buffer2)) != -1) {
                baos.write(buffer2, 0, len1);
            }
            System.out.println(baos.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert baos != null;
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    @Test
    public void server() {

        ServerSocket socket = null;
        Socket accept = null;
        InputStream is = null;
        BufferedOutputStream bos = null;
        try {
            socket = new ServerSocket(9010);
            accept = socket.accept();

            is = accept.getInputStream();

            bos = new BufferedOutputStream(new FileOutputStream("photo2.png"));

            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }

            //服务器端给予客户端反馈
            OutputStream outputStream = accept.getOutputStream();
            outputStream.write("你好，照片已收到".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert bos != null;
                bos.close();
                is.close();
                accept.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
